﻿using MesErreurs;
using MySql.Data.MySqlClient;
using Persistance;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Metier
{
    public class Article
    {
        //Definition des attributs
        private String noArticle;
        private String libArticle;
        private int qteDispo;
        private String villeArticle;
        private float prixArticle;
        private String interrompu;
        //Definition des properties
        public String LibArticle
        {
            get { return libArticle; }
            set { libArticle = value; }
        }

        public String NoArticle
        {
            get { return noArticle; }
            set { noArticle = value; }
        }

        public int QteDispo
        {
            get { return qteDispo; }
            set { qteDispo = value; }
        }

        public String VilleArticle
        {
            get { return villeArticle; }
            set { villeArticle = value; }
        }

        public float PrixArticle
        {
            get { return prixArticle; }
            set { prixArticle = value; }
        }

        public String Interrompu
        {
            get { return interrompu; }
            set { interrompu = value; }
        }

        //Constructeurs
        public Article()
        {
            noArticle = "";
            libArticle = "";
            qteDispo = 0;
            villeArticle = "";
            prixArticle = 0;
            interrompu = "";
        }
        public Article(string noArt, string libArt, int qte, string ville, float prix, string inter)
        {
            noArticle = noArt;
            libArticle = libArt;
            qteDispo = qte;
            villeArticle = ville;
            prixArticle = prix;
            interrompu = inter;
        }

        //Traitement de la classe Article
        /// <summary>
        /// Lire un utilisateur sur son ID
        /// </summary>
        /// <param name="id_utilisateur">N° de l'utilisateur à lire</param>
        public List<String> LectureNoArticle( )
        {
            List<String> mesNumeros = new List<String>();
            DataTable dt;
            sErreurs er = new sErreurs("Erreur sur lecture du client.", "Utilisateur.LectureNoArticle()");
            try
            {
                String mysql = "SELECT DISTINCT NO_ARTICLE FROM ARTICLES ORDER BY NO_ARTICLE";
                dt = DbInterface.Lecture(mysql, er);
                foreach (DataRow dataRow in dt.Rows)
                {
                    mesNumeros.Add((dataRow[0]).ToString());
                }
                return mesNumeros;
            }
            catch (MySqlException e)
            {
                throw new MonException(er.MessageUtilisateur(), er.MessageApplication(),
                e.Message);
            }
        }

        public Article RechercheUnArticle(String numArt)
        {
            String mysql;
            DataTable dt;
            sErreurs er = new sErreurs("Erreur sur recherche d'un article.", "Client.RechercheUnArticle()");
            try
            {
                mysql = "SELECT LIB_ARTICLE, QTE_DISPO, VILLE_ART,";
                mysql += "PRIX_ART, INTERROMPU ";
                mysql += "FROM ARTICLES WHERE NO_ARTICLE=':numero'";
                // On se protège de l’injection sql
                mysql = mysql.Replace(":numero", numArt);
                // On appelle la couche Persistance
                dt = DbInterface.Lecture(mysql, er);
                if (dt.IsInitialized)
                {
                    DataRow dataRow = dt.Rows[0];
                    this.noArticle = numArt;
                    this.qteDispo = (int)dataRow[1];
                    this.libArticle = dataRow[0].ToString();
                    this.villeArticle = dataRow[2].ToString();
                    this.prixArticle = Convert.ToSingle(dataRow[3]);
                    this.interrompu = dataRow[4].ToString();
                    return this;
                }
                else return null;
            }
            catch (MySqlException e)
            {
                throw new MonException(er.MessageUtilisateur(), er.MessageApplication(),
                e.Message);
            }
        }

        public List<Article> getLesArticles()
        {
            DataTable dt;
            sErreurs er = new sErreurs("Erreur sur recherche des articles.", "Article.getLesArticles()");
            String mysql = "SELECT * FROM Articles";
            try
            {
                dt = DbInterface.Lecture(mysql, er);
                List<Article> mesArts = new List<Article>();
                foreach (DataRow dataRow in dt.Rows)
                {
                    Article unart = new Article(dataRow[0].ToString(), (dataRow[1].ToString()), (int)dataRow[2], dataRow[3].ToString(), Convert.ToSingle(dataRow[4]),dataRow[5].ToString());
                    mesArts.Add(unart);
                }
                return mesArts;
            }
            catch (MonException erreur)
            {
                throw erreur;
            }
        }

        public void insererArticle(string noArt, string lib, string qte, string ville, string prix)
        {
            String requete = "INSERT INTO ARTICLES VALUES (':numArt',':libArt',:qtedispo,':ville',:prix,':interrompu');";
            requete = requete.Replace(":numArt", noArt);
            requete = requete.Replace(":libArt", lib);
            requete = requete.Replace(":qtedispo", qte);
            requete = requete.Replace(":ville", ville);
            prix = prix.Replace(",", ".");
            requete = requete.Replace(":prix", prix);
            requete = requete.Replace(":interrompu", "F");

            sErreurs er = new sErreurs("Erreur sur l'insertion de la commande.", "Utilisateur.insererCommande");
            try
            {
                DbInterface.Ecriture(requete, "INSERT");
            }
            catch (MySqlException e)
            {
                throw new MonException(er.MessageUtilisateur(), er.MessageApplication(),
                e.Message);
            }
        }

        public void modifierArticle()
        {
            String requete = "UPDATE ARTICLES SET LIB_ARTICLE=':numVend',QTE_DISPO=:quantite,VILLE_ART=':ville',PRIX_ART=:prix,INTERROMPU=':interrompu' WHERE NO_ARTICLE=':noArticle';";
            requete = requete.Replace(":noArticle", noArticle);
            requete = requete.Replace(":numVend", libArticle);
            requete = requete.Replace(":quantite", qteDispo.ToString());
            requete = requete.Replace(":ville", villeArticle);
            requete = requete.Replace(":prix", prixArticle.ToString());
            requete = requete.Replace(":interrompu", interrompu);

            sErreurs er = new sErreurs("Erreur sur la modification de l'article.", "Utilisateur.ModifierArticle");
            try
            {
                DbInterface.Ecriture(requete, "UPDATE");
            }
            catch (MySqlException e)
            {
                throw new MonException(er.MessageUtilisateur(), er.MessageApplication(),
                e.Message);
            }
        }

        public void supprimerArticle()
        {
            String requete = "DELETE FROM ARTICLES WHERE NO_ARTICLE=:noArticle;";
            requete = requete.Replace(":noArticle", noArticle);
            sErreurs er = new sErreurs("Erreur sur la suppression de l'article.", "Utilisateur.supprimerArticle");
            try
            {
                DbInterface.Ecriture(requete, "DELETE");
            }
            catch (MySqlException e)
            {
                throw new MonException(er.MessageUtilisateur(), er.MessageApplication(),
                e.Message);
            }
        }
    }
}
